﻿@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore
@using System.Collections.Generic
@using System.Linq
@@page
@@model @Model.NamespaceName.@Model.PageModelClassName

@{
    if (Model.IsPartialView)
    {
    }
    else if (Model.IsLayoutPageSelected)
    {
@:@@{
    @:ViewData["Title"] = "@Model.RazorPageName";
        if (!string.IsNullOrEmpty(Model.LayoutPageFile))
        {
    @:Layout = "@Model.LayoutPageFile";
        }
@:}
@:
@:<h1>@Model.RazorPageName</h1>
@:
    }
    else
    {
@:@@{
    @:Layout = null;
@:}
@:
@:<!DOCTYPE html>
@:
@:<html>
@:<head>
    @:<meta name="viewport" content="width=device-width" />
    @:<title>@Model.RazorPageName</title>
@:</head>
@:<body>
@:
        //    PushIndent("    ");
    }
@:<h4>@Model.ViewDataTypeShortName</h4>
@:<hr />
@:<div class="row">
    @:<div class="col-md-4">
        @:<form method="post">
            @:<div asp-validation-summary="ModelOnly" class="text-danger"></div>
        foreach (var property in Model.ModelMetadata.Properties)
        {
            if (property.Scaffold && !property.IsAutoGenerated && !property.IsReadOnly)
            {
                if (property.IsForeignKey)
                {
            @:<div class="form-group">
                @:<label asp-for="@(Model.ModelTypeName).@property.PropertyName" class="control-label"></label>
                @:<select asp-for="@(Model.ModelTypeName).@property.PropertyName" class ="form-control" asp-items="ViewBag.@property.PropertyName"></select>
            @:</div>
                    continue;
                }

                bool isCheckbox = property.TypeName.Equals("System.Boolean");
                if (isCheckbox)
                {
            @:<div class="form-group form-check">
                @:<label class="form-check-label">
                    @:<input class="form-check-input" asp-for="@(Model.ModelTypeName).@property.PropertyName" /> @@Html.DisplayNameFor(model => model.@(Model.ModelTypeName).@GetValueExpression(property))
                @:</label>
            @:</div>
                }
                else if (property.IsEnum && !property.IsEnumFlags)
                {
            @:<div class="form-group">
                @:<label asp-for="@(Model.ModelTypeName).@property.PropertyName" class="control-label"></label>
                @:<select asp-for="@(Model.ModelTypeName).@property.PropertyName" class="form-control"></select>
                @:<span asp-validation-for="@(Model.ModelTypeName).@property.PropertyName" class="text-danger"></span>
            @:</div>
                }
                else if (property.IsMultilineText)
                {
            @:<div class="form-group">
                @:<label asp-for="@(Model.ModelTypeName).@property.PropertyName" class="control-label"></label>
                @:<textarea asp-for="@(Model.ModelTypeName).@property.PropertyName" class="form-control"></textarea>
                @:<span asp-validation-for="@(Model.ModelTypeName).@property.PropertyName" class="text-danger"></span>
            @:</div>
                }
                else
                {
            @:<div class="form-group">
                @:<label asp-for="@(Model.ModelTypeName).@property.PropertyName" class="control-label"></label>
                @:<input asp-for="@(Model.ModelTypeName).@property.PropertyName" class="form-control" />
                @:<span asp-validation-for="@(Model.ModelTypeName).@property.PropertyName" class="text-danger"></span>
            @:</div>
                }
            }
        }
    }
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@{
    if (Model.ReferenceScriptLibraries)
    {
@:@@section Scripts {
    @:@@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
@:}
    }
    // The following code closes the tag used in the case of a view using a layout page and the body and html tags in the case of a regular view page
    if (!Model.IsPartialView && !Model.IsLayoutPageSelected)
    {
@:</body>
@:</html>
    }
}
@functions
{
    // Do we need to use this in conjunction with the PrimaryKey check?
    bool IsPropertyGuid(IPropertyMetadata property)
    {
        return string.Equals("System.Guid", property.TypeName, StringComparison.OrdinalIgnoreCase);
    }

    string GetValueExpression(IPropertyMetadata property)
    {
        return property.PropertyName;
    }
}
